home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / gfx / conv / wasp202b.lha / wasp / src / ppm.c < prev    next >
C/C++ Source or Header  |  1992-03-21  |  4KB  |  257 lines

  1. /* wasp - Copyright 1991 by Steven Reiz
  2.  * see COPYING and wasp.c for further info,
  3.  * ppm.c, 24/7/91, 22/10/91 - 25/10/91,
  4.  * 8/12/91, 27/12/91, 21/3/92
  5.  */
  6.  
  7. static char *sourcefile=__FILE__;
  8.  
  9. #include "wasp.h"
  10.  
  11. int
  12. read_ppm(void)
  13. {
  14.     int y;
  15.     long depth;
  16.     char typ[3];
  17.  
  18.     cread(typ, 3);
  19.     if (typ[0]!='P' || typ[1]<'0' || typ[1]>'9' || typ[2]!='\n') {
  20.         cseek_in(0L, 0);
  21.         return 0;
  22.     }
  23.     typ[2]='\0';
  24.     xsz=getn(' ');
  25.     ysz=getn('\n');
  26.     depth=getn('\n')+1;
  27.     printe("PPM/%s input; %ld x %ld, depth: %ld\n", typ, xsz, ysz, depth);
  28.     if (!outfilename)
  29.         exit(0);
  30.     if (depth!=256) {
  31.         printe("depths other than 256 are not supported\n");
  32.         exit(1);
  33.     }
  34.     rgb=Malloc(ysz*sizeof(u_short *));
  35.     for (y=0; y<ysz; ++y)
  36.             rgb[y]=Calloc(xsz*sizeof(u_short));
  37.     switch (typ[1]) {
  38.     case '5':
  39.         read_grey8();
  40.     break;
  41.     case '6':
  42.         read_rgb24();
  43.     break;
  44.     default:
  45.         printe("types other than P5 or P6 are not supported\n");
  46.         exit(1);
  47.     break;
  48.     }
  49.     return 1;
  50. }
  51.  
  52.  
  53. void
  54. write_ppm(void)
  55. {
  56.     printe("PPM/P6 output; %ld x %ld, depth: 256\n", xsz, ysz);
  57.     cwrite("P6\n", 3);
  58.     putn((int)xsz);
  59.     cwrite(" ", 1);
  60.     putn((int)ysz);
  61.     cwrite("\n255\n", 5);
  62.     write_rgb24();
  63.     erase_counter("PPM file written, %ld bytes", cseek_out(0L, 1));
  64. }
  65.  
  66.  
  67. int
  68. getn(char terminator)
  69. {
  70.     int n;
  71.     char c;
  72.  
  73.     n=0;
  74.     while (1) {
  75.         cread(&c, 1);
  76.         if (c<'0' || c>'9')
  77.             break;
  78.         n=10*n+c-'0';
  79.     }
  80.     assert(c==terminator);
  81.     return n;
  82. }
  83.  
  84.  
  85. void
  86. putn(int n)
  87. {
  88.     int i;
  89.     char line[20];
  90.  
  91.     i=19;
  92.     do {
  93.         line[i--]=n%10+'0';
  94.         n/=10;
  95.     } while (n>0);
  96.     cwrite(line+i+1, 19-i);
  97. }
  98.  
  99.  
  100. int
  101. read_hl(void)
  102. {
  103.     int y;
  104.     char typ[3];
  105.     short n;
  106.     long lon;
  107.  
  108.     cread(typ, 3);
  109.     if (typ[0]!='H' || typ[1]!='L' || typ[2]!='2') {
  110.         cseek_in(0L, 0);
  111.         return 0;
  112.     }
  113.     cread(&n, 2);
  114.     xsz=n;
  115.     cread(&n, 2);
  116.     ysz=n;
  117.     cread(&lon, 4);
  118.     printe("HL2 input; %ld x %ld\n", xsz, ysz);
  119.     if (!outfilename)
  120.         exit(0);
  121.     rgb=Malloc(ysz*sizeof(u_short *));
  122.     for (y=0; y<ysz; ++y)
  123.             rgb[y]=Calloc(xsz*sizeof(u_short));
  124.     read_rgb24();
  125.     return 1;
  126. }
  127.  
  128.  
  129. int
  130. read_mtv(void)
  131. {
  132.     int y, i;
  133. #define TESTBUFSZ 20
  134.     char testbuf[TESTBUFSZ];
  135.  
  136.     cread_type=CREAD_OPTIONAL;
  137.     cread(testbuf, TESTBUFSZ);
  138.     if (testbuf[0]<'0' || testbuf[0]>'9') {
  139.         cseek_in(0L, 0);
  140.         return 0;
  141.     }
  142.     for (i=1; i<cread_result; ++i) {
  143.         if (testbuf[i]=='\n')
  144.             break;
  145.         if (testbuf[i]!=' ' && (testbuf[i]<'0' || testbuf[i]>'9')) {
  146.             cseek_in(0L, 0);
  147.             return 0;
  148.         }
  149.     }
  150.     cseek_in(0L, 0);
  151.     xsz=getn(' ');
  152.     ysz=getn('\n');
  153.     printe("MTV input; %ld x %ld\n", xsz, ysz);
  154.     if (!outfilename)
  155.         exit(0);
  156.     rgb=Malloc(ysz*sizeof(u_short *));
  157.     for (y=0; y<ysz; ++y)
  158.             rgb[y]=Calloc(xsz*sizeof(u_short));
  159.     read_rgb24();
  160.     return 1;
  161. }
  162.  
  163.  
  164. void
  165. read_rgb24(void)
  166. {
  167.     char *buf;
  168.     int width, color;
  169.     int y, x;
  170.     char *bufp;
  171.     u_short *p;
  172.  
  173.     cread_type=CREAD_NONFATAL;
  174.     width=xsz*3;
  175.     buf=Malloc(width);
  176.     init_counter(0, (int)ysz, 10, "reading 24-bits RGB");
  177.     for (y=0; y<ysz; ++y) {
  178.         counter();
  179.         cread(buf, width);
  180.         if (cread_result!=width)
  181.             break;
  182.         x=xsz-1;
  183.         bufp=buf;
  184.         p=rgb[y];
  185.         do {
  186.             color=(*bufp++ & 0xf0)<<4;
  187.             color|= *bufp++ & 0xf0;
  188.             color|=(*bufp++ >>4) & 0x0f;
  189.             *p++ =color;
  190.         } while (--x>=0);
  191.     }
  192.     erase_counter(NULL);
  193.     free(buf);
  194.     cread_type=CREAD_STRICT;
  195. }
  196.  
  197.  
  198. void
  199. read_grey8(void)
  200. {
  201.     char *buf;
  202.     int color;
  203.     int y, x;
  204.     char *bufp;
  205.     u_short *p;
  206.  
  207.     cread_type=CREAD_NONFATAL;
  208.     buf=Malloc((int)xsz);
  209.     init_counter(0, (int)ysz, 10, "reading 8-bits grey");
  210.     for (y=0; y<ysz; ++y) {
  211.         counter();
  212.         cread(buf, (int)xsz);
  213.         if (cread_result!=xsz)
  214.             break;
  215.         x=xsz-1;
  216.         bufp=buf;
  217.         p=rgb[y];
  218.         do {
  219.             color= *bufp++ & 0xf0;
  220.             color|=(color<<4)|(color>>4);
  221.             *p++ =color;
  222.         } while (--x>=0);
  223.     }
  224.     erase_counter(NULL);
  225.     free(buf);
  226.     cread_type=CREAD_STRICT;
  227. }
  228.  
  229.  
  230. void
  231. write_rgb24(void)
  232. {
  233.     char *buf;
  234.     int width, color;
  235.     int y, x;
  236.     char *bufp;
  237.     u_short *p;
  238.     
  239.     width=xsz*3;
  240.     buf=Malloc(width);
  241.     init_counter(0, (int)ysz, 10, "writing 24-bits RGB");
  242.     for (y=0; y<ysz; ++y) {
  243.         counter();
  244.         x=xsz-1;
  245.         bufp=buf;
  246.         p=rgb[y];
  247.         do {
  248.             color= *p++;
  249.             *bufp++ =(color>>4)&0xf0;
  250.             *bufp++ =color&0xf0;
  251.             *bufp++ =color<<4;
  252.         } while (--x>=0);
  253.         cwrite(buf, width);
  254.     }
  255.     free(buf);
  256. }
  257.